<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="查找任务名">
                <a-input v-model="queryParam.task_name" placeholder="请输入用户名包含的字母"/>
              </a-form-item>
            </a-col>

            <a-col :md="4" :sm="24">
              <a-form-item label="查找任务状态">
                <a-select v-model="queryParam.status" placeholder="请选择" default-value="1">
                  <a-select-option value="1">未完成</a-select-option>
                  <a-select-option value="2">完成</a-select-option>
                  <a-select-option value="-1">删除</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>

            <a-col :md="6" :sm="24">
              <a-form-item label="查找任务标签">
                <a-select
                  v-model="queryParam.taskcate_id"
                  show-search
                  placeholder="请选择"
                  option-filter-prop="children"
                  :filter-option="filterOption"
                >
                  <a-select-option :value="item.id" v-for="(item) in labelList" :key="item.id">
                      {{item.title}}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>

            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="reset">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>


      <s-table
        ref="table"
        size="default"
        :rowKey="(record,index)=>{return index}"
        :columns="columns"
        :data="loadData"
        showPagination="auto"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>

        <span slot="files" slot-scope="text, record">
          <!-- <a href="#" target="_slef">查看附件内容</a> -->
          <a :href="record.files == '' ? '#' : record.files" :target="record.files == ''? '' :'_bla nk'" @click="handle(record)">查看附件内容</a>
        </span>

        <span slot="unfinish_num" slot-scope="text, record">
          <a @click="handleUn(record)">{{record.unfinish_num}}</a>
        </span>

        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleEdit(record)" v-action:tasklist_edit2>修改</a>
            <a-divider type="vertical" />
            <a @click="handleWatch(record)" v-action:tasklist_add2>查看学校</a>
            <a-divider type="vertical" v-if="record.status == 1"/>
            <!-- <a @click="handleSub(record)">修改表单</a>
            <a-divider type="vertical" /> -->
              <a-popconfirm v-if="record.status == 1"
                v-action:tasklist_del2
                title="确定撤销吗?"
                ok-text="确定"
                cancel-text="取消"
                @confirm="confirm(record)"
                @cancel="cancel(record)"
              >
              <a>撤销</a>
            </a-popconfirm>
            <a-divider type="vertical" v-if="record.status == -1"/>
              <a-popconfirm v-if="record.status == -1"
                v-action:tasklist_del2
                title="确定恢复吗?"
                ok-text="确定"
                cancel-text="取消"
                @confirm="confirms(record)"
                @cancel="cancels(record)"
              >
              <a>恢复</a>
            </a-popconfirm>
          </template>
        </span>
      </s-table>

      <create-form
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        @getfiles="getfiles"
        @cancel="handleCancel"
        @ok="handleOk"
      />

        <updata-form
        ref="createModal"
        :visible="updata"
        :loading="confirmLoading"
        :model="mdl"
        @cancel="handleUpdataCancel"
        @ok="handleUpdataOk"
      />

      <un-school
        :visible="unschool"
        :loading="unLoading"
        :model="mdls"
        @cancel="handleCancelUnschool"
      />
      <step-by-step-modal ref="modal" @ok="handleOk"/>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import { GetTaskListOne, UpdataTaskListOne,DelTaskListOne, GetTaskList } from '../../../api/school'

import StepByStepModal from './modules/StepByStepModal'
import CreateForm from './modules/CreateForm'
import UpdataForm from './modules/UpdataForm'
import UnSchool from './modules/UnSchool'

const columns = [
  {
    title: '编号',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '任务名称',
    dataIndex: 'task_name'
  },
  {
    title: '发布人姓名',
    dataIndex: 'username'
  },
  {
    title: '发布时间',
    dataIndex: 'release_time',
  },
  {
    title: '截止时间',
    dataIndex: 'endtime',
  },
  {
    title: '分类名称',
    dataIndex: 'title',
  },
  {
    title: '任务描述',
    dataIndex: 'descs',
  },
  {
    title: '附件',
    dataIndex: 'files',
    width: '180px',
    scopedSlots: { customRender: 'files' }
  },
  {
    title: ' 任务目标',
    dataIndex: 'task_target',
  },
  {
    title: '未完成数量',
    dataIndex: 'unfinish_num',
    scopedSlots: { customRender: 'unfinish_num' }
  },
  {
    title: ' 完成数量',
    dataIndex: 'finish_num',
  },
  {
    title: '状态',
    dataIndex: 'statusname'
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '180px',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'tasklist',
  components: {
    STable,
    Ellipsis,
    CreateForm,
    StepByStepModal,
    UpdataForm,
    UnSchool,
  },
  data () {
    this.columns = columns
    return {
      // un school
      unschool: false,
      unLoading: false,
      mdls: null,
      // 附件
      files: '',
      labelList: [],
      index: 0,
      // 动态表单数据
      dataList: [],
      // updata model
      updata: false,
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam, {role_type: localStorage.getItem('role_type')})
        console.log('loadData request parameters:', requestParameters)
        return GetTaskListOne(requestParameters)
          .then(res => {
            return res.data
          })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  created() {
    // 调用方法获取标签列表
    this.UsergetDepartmentList()
  },
  computed: {
    rowSelection () {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    handle(handle) {
      if(!handle.files) {
        this.$message.info('暂无附件') 
      }
    },
    // 未完成学校数量
    handleUn(record) {
      this.unschool = true
      this.mdls = { ...record }
    },
    getfiles(value) {
      console.log('-------------value', value)
      this.files = value
    },
    // 获取标签列表
    UsergetDepartmentList () {
      GetTaskList().then(res => {
        this.labelList = res.data.data
      })
    },
    getdata(value) {
      this.dataList = value

    },
    handleAdd () {
      this.mdl = null
      this.visible = true
    },
    handleEdit (record) {
      this.visible = true
      this.mdl = { ...record }
      this.mdl.aaa = [{
          uid: record.id,
          name: record.files,
          status: 'done',
          url: record.files
        }]
    },
    // 查看学校信息
    handleWatch (record) {
      this.updata = true
      this.mdl = { ...record }
    },
    // 撤销
    confirm(e) {
      const form = this.$refs.createModal.form
      console.log(e);
      DelTaskListOne({id: e.id, endtime: e.endtime}).then(res => {
        // 重置表单数据
        form.resetFields()
        // 刷新表格
        this.$refs.table.refresh()
        this.$message.success('撤销成功');
      }).catch(error => {
        console.log('-------------error------------', error)
      })
    },
    cancel(e) {
      console.log(e);
      this.$message.error('已取消'); 
    },
    // 恢复
      confirms(e) {
      const form = this.$refs.createModal.form
      console.log(e);
      UpdataTaskListOne({id: e.id, status: e.status}).then(res => {
        // 重置表单数据
        form.resetFields()
        // 刷新表格
        this.$refs.table.refresh()
        this.$message.success('恢复成功');
      }).catch(error => {
        console.log('-------------error------------', error)
      })
    },
    cancels(e) {
      console.log(e);
      this.$message.error('已取消');
    },
    // 查看
    handleUpdataCancel() {
      this.updata = false
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    // 查看未完成的学校信息
    handleCancelUnschool() {
      this.unschool = false
    },
    handleUpdataOk() {},
    // 创建
    handleOk () {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          console.log('values', values)
          if (values.id > 0) {
            // 修改 e.g.
            new Promise((resolve, reject) => {
              values.files = this.files
              UpdataTaskListOne(values).then(res => {
                resolve()
              }, reason => {
                this.$message.info('任务名已存在')
                this.confirmLoading = false
              })
            }).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.success('修改成功')
            })
          } else {
            // 新增
            new Promise((resolve, reject) => {
              values.files = this.files
              AddUser(values).then(res => {
                reason()
              }, reason => {
                this.confirmLoading = false
              })
            }).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.success('新增成功')
            })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.visible = false

      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    // 显示修改密码窗口
    handleSub (record) {
      this.$router.push({
        name: 'UpdataForm',
        path: '/task/UpdataForm',
        query: {data: JSON.stringify(record)}
      })
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    resetSearchForm () {
      this.queryParam = {
        date: moment(new Date())
      }
    },
    // 重置
    reset () {
      const form = this.$refs.createModal.form
      this.queryParam = {}
        // 重置表单数据
      form.resetFields()
      // 刷新表格
      this.$refs.table.refresh()
    },
    // 搜素模糊查询
    filterOption(input, option) {
      return (
    option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    }
  }
}
</script>
